require 'open-uri'
require 'rubygems'
require 'hpricot'
require 'htmlentities'
require 'iconv'
IRCBot::Plugin.define 'define' do
	help_string 'Looks up words in the Merriam-Webster (American English) dictionary. Use like "define word"'
	
	def command(user,target,message,hostmask)
		Thread.new do
			if !message.empty? then
				doc = Hpricot(open("http://www.merriam-webster.com/dictionary/#{message.downcase.strip}"))
				if doc.at('#mwEntryData') and doc.at('#mwEntryData').at('p.d') then
					paragraph = doc.at('#mwEntryData').at('p.d').inner_html
					definition = HTMLEntities.new.decode(paragraph.gsub("<br />",' ').gsub("\n"," ").gsub("<strong>",2.chr).gsub("</strong>",2.chr).gsub(/<a(.+?)>/,0x1F.chr).gsub("</a>",0x1F.chr).gsub(/<(.+?)>/,""))
					privmsg(target,definition)
				else
					privmsg(target,"That isn't in the dictionary.")
				end
			else
				privmsg(target,"Hey, I need a word to look up.")
			end
		end
	end
end
